<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Team;
use App\Models\Tea;
use App\Models\Teacher;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use App\Admin\Renderable\Players;
use Dcat\Admin\Support\Helper;
use function foo\func;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;

class TeamController extends AdminController
{

    public function index(Content $content)
    {
        return $content->title('球队')
            ->description('列表')
            ->body($this->grid());
    }

    public function create(Content $content)
    {
        return $content->title('球队')
            ->description('新增')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Team(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('team_name');
            $grid->column('introduce','球队介绍')->display(function ($value){
                return str_limit($value,20);
            });
            $grid->column('coach_id','主教练')->display(function ($id){
               return Teacher::getNameById($id);
            });
            $grid->column('motto','球队格言');
//            $grid->column('tea_log','球队Logo')->image('',100,100);
            $grid->tea_log('球队Logo')->image(config('alioss.ossServer'), 50, 50);
            $grid->team_img('球队合照')->display(function ($pictures) {
                return json_decode($pictures, true);
            })->image(config('alioss.ossServer'), 50, 50);
            $grid->column('establish','成立时间');
            $grid->team_status('球队状态')->switch();
            $grid->column('period','球队课时')->editable(true);
                $grid->member->display('查看球员')->modal('球员列表', Players::make());
            $grid->column('created_at')->hide();
            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                $filter->like('team_name', '球队名称')->width(4);
            });


            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
//                $actions->disableEdit();
//                $actions->disableQuickEidt();
                $actions->disableView();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Team(), function (Show $show) {
            $show->field('id');
            $show->field('team_name');
            $show->field('member');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Team(), function (Form $form) {
            $form->display('id');
            $form->text('team_name');
            $form->select('coach_id','主教练')->options('teacher/teacherDate');
            $form->text('motto','球队格言');
            $form->file('tea_log','球队Logo')->autoUpload()->uniqueName();
            $form->switch('team_status','球队状态')->default(1);
//            $form->multipleSelect('member','添加球员')->options('user/ownerDate')->saving(function ($value) {
//                // 转化成json字符串保存到数据库
//                if (!empty($value)){
//                    return json_encode($value);
//                }
//                return 0;
//            });

            $form->multipleFile('team_img','精彩图集')->saving(function ($paths) {
                // 也可以转化为json
                return json_encode($paths);
            })->autoUpload()->uniqueName();
            $form->text('period','球队课时');
            $form->text('introduce','球队介绍');
            $form->date('establish','成立时间');
            $form->display('created_at');
            $form->display('updated_at');
//            $form->saved(function (Form $form,$result) {
//                if ($form->isCreating()) {
//                    $team_id  = $result;
//                    $new_user = [];
//                    $user = $form->member[0];
//                    if ($user) {
//                        foreach ($form->member as $k => $v) {
//                            if ($v) {
//                                $new_user[] = $v;
//                            }
//                        }
//                        $res = DB::table('user')->whereIn('id', $new_user)->update(['team_id' => $team_id, 'update_time' => time()]);
//                        if ($res) {
//                            return $form->redirect('Team', '保存成功');
//                        } else {
//                            return $form->error('数据保存失败');
//                        }
//                    }
//                }
//
//            });
        });
    }

    public function teamData(){
        $teamDate = \App\Models\Team::where(['team_status'=>1])->get(['id','team_name as text'])->toArray();
        return $teamDate;
    }



    protected function information_info(Request $request)
    {
        $url = $request->url();//获取url
        $id = str_replace('/', '', strrchr($url, '/'));//获取url传参
        $id_ley = $request->input('placement');//获取状态值
        //直接进行数据库更新操作，然后返回，在返回时需要注意传参
//        var_dump($id,$id_ley);die;
        $arr = [
            'status' => true,
            'message' => "更新成功 !",
            'redirect' => admin_url('information_info/' . $id),
        ];
        return $arr;
    }
}
